<template>
  <div class="login">
    <div class="login-text">欢迎登入</div>
    <van-form @submit="onSubmit" class="login-form">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" />
      </van-cell-group>
      <div style="margin: 0.1rem">
        <van-button round block type="primary" native-type="submit"> 登入 </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from "vue";
import { useStore } from "../stores";
import { getlogin } from "../api/user";
import { useRouter } from "vue-router";

export default {
  setup() {
    const store = useStore();
    const state = reactive({});
    const router = useRouter();

    const username = ref("");
    const password = ref("");
    const onSubmit = (values) => {
      getlogin({
        email: username.value,
        password: password.value,
      })
        .then((res) => {
          console.log(res.data.token);
          store.gettoken(res.data.token);
          console.log("111");
          router.push({ path: "/" });
          store.getfootmu(true);
          //router.go(-1);
        })
        .catch((err) => {
          console.log(err);
          //alert("用户名或密码错误");
        });
      // console.log("submit", values);
      // console.log(username.value);
      // console.log(password.value);
      console.log(store.token);
    };
    onMounted(() => {
      store.getfootmu(false);
    });

    return {
      username,
      password,
      onSubmit,
      state,
      store,
      router,
    };
  },
};
</script>
<style lang="less" scoped>
.login {
  .login-form {
    //width: 80%;
    margin-top: 3rem;
  }
}
.login-text {
  font-size: 1rem;
  text-align: center;
  margin-top: 2rem;
}
</style>
